package com.airbnb.mvrx.sample.features.clickread.views

import android.content.Context
import android.view.View
import android.widget.FrameLayout
import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView
import com.example.mvrxstudy.R
import com.yt.ytdeep.client.dto.ClickReadPage

@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT)
class CRContentContainerView @JvmOverloads constructor(context: Context)
    : FrameLayout(context) {

    private val mPageContent: CRContentView
    private val mPageBottom: CRBottomView
    private var isFastLocationScroll: Boolean = false

    init {
        inflate(context, R.layout.view_content_container_cr, this)
        mPageContent = findViewById(R.id.v_page_content)
        mPageBottom = findViewById(R.id.v_page_bottom)
        mPageContent.setOnScrollListener(object : SnappingRecyclerView.OnViewSelectedListener() {
            override fun onSelected(view: View?, position: Int) {
                if (!isFastLocationScroll) {
                    mPageBottom.setPosition(position)
                }
                isFastLocationScroll = false
            }
        })
        mPageBottom.setOnViewSelectedListener(object : SnappingRecyclerView.OnViewSelectedListener() {
            override fun onSelected(view: View?, position: Int) {
                isFastLocationScroll = true
                mPageContent.setPosition(position)
            }
        })
    }

    @ModelProp
    fun setContent(pages: List<ClickReadPage>) {
        mPageContent.setContent(pages)
        mPageBottom.setThumbs(pages)
    }

}